package com.ruoyi.inspection.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.common.enums.YnStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.config.GovernTaskConfig;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.inspection.domain.receiveVo.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.entity.LightningData;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api("任务上报接口")
@RequestMapping("/govern/push/")
public class GovernOtherTaskPush {
    @Resource
    private PushInfoMapper pushInfoMapper;
    @Autowired
    private LightningDataMapper lightningDataMapper;
    @Autowired
    private ILightningWarnService lightningWarnService;



    /*******************雷电预警数据 * 每间隔30分钟推送一次 *雷电预警数据 ***************/
    @Scheduled(cron = "0 0/30 * * * ?")
    public void lightningWarnScheduleTask() {
        pushLightningWarnData();
    }

    /*******************风力、气候数据 * 每间隔10分钟推送一次 *风力、气候数据 ***************/
    @Scheduled(cron = "0 0/10 * * * ?")
    public void enviromentMonitorScheduleTask() {
        pushEnviromentMonitorData();
    }


    /**
     * 推送雷电预警基础设备信息
     * 静态设备信息，只推送一次
     */
    public void pushThunderDeviceEquip() {
        ReceiveThunderWarningEquip equip = new ReceiveThunderWarningEquip();
        equip.setId(UUID.randomUUID().toString());
        equip.setCompanyCode(GlobalConstants.COMPANY_CODE);
        equip.setEquipCode(GlobalConstants.COMPANY_CODE + "100");
        equip.setLongitude("121.403736");
        equip.setLatitude("31.471671");
        equip.setEquipName("大气电场仪");
        equip.setRadius("1");
        equip.setDeleted("0");
        equip.setCreateBy("admin");
        equip.setUpdateBy("admin");
        equip.setCreateDate("2022-10-29 17:39:06");
        equip.setUpdateDate("2024-10-29 17:39:06");

        List<ReceiveThunderWarningEquip> resultList = new ArrayList<>();
        resultList.add(equip);

        // 市局推送雷电预警基础设备信息
        String url = GovernTaskConfig.receiveThunderWarningEquipList;
        String s = GovernTaskConfig.pushData(JSONUtil.toJsonPrettyStr(resultList), url);
        pushInfoMapper.insert(s);
    }

    /**
     * 上报视频智能分析数据 到 市局
     */
    public void pushVideoAlarm() {
        ReceiveVideoAlarm videoAlarm = new ReceiveVideoAlarm();
        videoAlarm.setId(UUID.randomUUID().toString());
        videoAlarm.setCompanyCode(GlobalConstants.COMPANY_CODE);
        videoAlarm.setVideoCode("31017000301320000003");
        videoAlarm.setVideoAlarmType("2");
        videoAlarm.setAlarmStatus(1);
        videoAlarm.setAlarmTime("2024-12-08 12:14:47");
        videoAlarm.setDeleted("0");

        List<ReceiveVideoAlarm> resultList = new ArrayList<>();
        resultList.add(videoAlarm);

        // 市局上报视频智能分析数据
        String url = GovernTaskConfig.receiveVideoAlarmList;
        String s = GovernTaskConfig.pushData(JSONUtil.toJsonPrettyStr(resultList), url);

        pushInfoMapper.insert(s);

    }

    /**
     * 推送雷电预警数据
     */
    public void pushLightningWarnData() {
        LightningWarn searchEntity = new LightningWarn();
        // 是否已推送市局:0否 1是
        searchEntity.setIsUpload(0);
        // 查询待推送的雷电预警信息
        List<LightningWarn> lightningDataList =
                lightningWarnService.selectLightningWarnList(searchEntity);
        if (StringUtils.isEmpty(lightningDataList)) {
            log.info("推送市局端 : 没有要推送的雷电预警信息");
            return ;
        }

        List<ReceiveThunderWarning> resultList = lightningDataList.stream()
                .map(ReceiveThunderWarning::lightningDataToReceiveThunderWarning)
                .collect(Collectors.toList());

        // 市局推送雷电预警信息
        String url = GovernTaskConfig.receiveThunderWarningList;
        String s = GovernTaskConfig.pushData(JSONUtil.toJsonPrettyStr(resultList), url);
        pushInfoMapper.insert(s);
        lightningDataList.forEach(lightningWarn -> {
            LightningWarn update = new LightningWarn();
            update.setId(lightningWarn.getId());
            update.setIsUpload(1);
            lightningWarnService.updateLightningWarn(update);
        });
    }

    /**
     * 推送环境监测数据
     */
    public void pushEnviromentMonitorData() {
        LightningData lightningData = new LightningData();
        // 是否已推送市局： 0否 1是
        lightningData.setIsUpload(0);
        // 查询未推送的风力监测数据
        List<LightningData> lightningDataList =
                this.lightningDataMapper.queryAllByLimit(lightningData);
        if (StringUtils.isEmpty(lightningDataList)) {
            log.info("推送市局端 : 没有要推送的环境监测数据");
            return ;
        }

        List<ReceiveEnvironMonitor> resultList = lightningDataList.stream()
                .map(ReceiveEnvironMonitor::lightningDataToReceiveEnvironMonitor)
                .collect(Collectors.toList());

        // 市局推送环境监测数据
        String url = GovernTaskConfig.receiveThunderEnvironMonitorList;
        String s = GovernTaskConfig.pushData(JSONUtil.toJsonPrettyStr(resultList), url);
        pushInfoMapper.insert(s);
        lightningDataList.forEach(wind -> {
            LightningData update = new LightningData();
            update.setId(wind.getId());
            update.setIsUpload(1);
            lightningDataMapper.update(update);
        });
    }


}
